package com.bsj.power.common.config.cached;

import com.alibaba.fastjson2.JSON;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.def.entity.path.Path;
import com.bsj.power.common.def.entity.personnel.Personnel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description 负责人员缓存
 * @time 2024/5/8 11:42
 */
@Component
public class PersonnelCached {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    /**
     * 获取人员信息
     *
     * @param empNo
     * @return com.bsj.power.common.def.entity.personnel.Personnel
     * @author ljx
     * @time 2024/5/8 12:02
     */
    public Personnel hgetPersonnelInfo(String empNo) {
        String personnelStr = jedisClusterBSJ.hget(RedisConstant.PERSONNEL_REDIS_KEY, empNo);
        if (personnelStr == null) {
            return null;
        }
        return JSON.parseObject(personnelStr, Personnel.class);
    }

    /**
     * 缓存人员信息
     *
     * @param personnel
     * @return void
     * @author ljx
     * @time 2024/5/8 11:58
     */
    public void hsetPersonnelInfo(Personnel personnel) {
        if (personnel != null) {
            jedisClusterBSJ.hset(RedisConstant.PERSONNEL_REDIS_KEY, personnel.getEmpNo(), JSON.toJSONString(personnel));
        }
    }

    /**
     * 批量缓存人员信息
     *
     * @param personnelList
     * @return void
     * @author ljx
     * @time 2024/7/17 14:53
     */
    public void hsetAllPersonnelInfo(List<Personnel> personnelList) {
        if (!CollectionUtils.isEmpty(personnelList)) {
            personnelList.forEach(personnel -> {
                jedisClusterBSJ.hset(RedisConstant.PERSONNEL_REDIS_KEY, personnel.getEmpNo(), JSON.toJSONString(personnel));
            });
        }
    }

    /**
     * 删除人员信息
     *
     * @param empNo
     * @return void
     * @author ljx
     * @time 2024/5/8 11:59
     */
    public void hdelPersonnelInfo(String empNo) {
        jedisClusterBSJ.hdel(RedisConstant.PERSONNEL_REDIS_KEY, empNo);
    }

    /**
     * 获取所有人员信息
     *
     * @return java.util.List<com.bsj.power.common.def.entity.personnel.Personnel>
     * @author ljx
     * @time 2024/7/8 12:00
     */
    public List<Personnel> hgetAllPersonnelInfo() {
        Map<String, String> personnelMap = jedisClusterBSJ.hgetAll(RedisConstant.PERSONNEL_REDIS_KEY);
        if (CollectionUtils.isEmpty(personnelMap)) {
            return null;
        }
        return personnelMap.entrySet().stream().map(value -> {
            return JSON.parseObject(value.getValue(), Personnel.class);
        }).collect(Collectors.toList());
    }

}
